﻿using System;
using System.Collections.Generic;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.Foundation;
using Windows.UI.Xaml.Media.Animation;
namespace FriendAnalyzer.UIHelpers
{
    public class RadGrid : Grid
    {
        public List<RadGrid> children;
        public RadGrid parent;
        public bool HasChildren;
        public RadGrid()
        {
            this.PointerPressed += RadGrid_PointerPressed;
            this.ManipulationMode = Windows.UI.Xaml.Input.ManipulationModes.TranslateX | ManipulationModes.TranslateY;
            this.ManipulationStarting += new ManipulationStartingEventHandler(ManipulateMe_ManipulationStarting);
            this.ManipulationStarted += new ManipulationStartedEventHandler(ManipulateMe_ManipulationStarted);
            this.ManipulationDelta += new ManipulationDeltaEventHandler(ManipulateMe_ManipulationDelta);
            this.ManipulationCompleted += new ManipulationCompletedEventHandler(ManipulateMe_ManipulationCompleted);
            this.ManipulationInertiaStarting += new ManipulationInertiaStartingEventHandler(ManipulateMe_ManipulationInertiaStarting);
            InitManipulationTransforms(this);
        }
        void RadGrid_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            //Storyboard sb = new Storyboard();
            //var db = new DoubleAnimation();
            //db.EasingFunction = new SineEase();
            //db.Duration = TimeSpan.FromSeconds(5);
            //db.From = 0;
            //db.To = 5;
            //var nd = (first[i].Node as SpotNode).NodeUI;
            //TranslateTransform t = new TranslateTransform();
            //t.X = first[i].Node.X;
            //t.Y = first[i].Node.Y;
            //nd.RenderTransform = t;
            //nd.UpdateLayout();
            //Storyboard.SetTarget(db, t);
            //Storyboard.SetTargetProperty(db, "TranslateTransform.X");
            //sb.Children.Add(db);
            //sb.Begin();
            if (HasChildren)
            {
                
            }
        }
        private TransformGroup _transformGroup;
        private MatrixTransform _previousTransform;
        private CompositeTransform _compositeTransform;
        private bool forceManipulationsToEnd;
        private void InitManipulationTransforms(Grid sender)
        {
            _transformGroup = new TransformGroup();
            _compositeTransform = new CompositeTransform();
            _previousTransform = new MatrixTransform() { Matrix = Matrix.Identity };
            _transformGroup.Children.Add(_previousTransform);
            _transformGroup.Children.Add(_compositeTransform);
            sender.RenderTransform = _transformGroup;
        }
        void ManipulateMe_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
        {
            forceManipulationsToEnd = false;
            e.Handled = true;
        }
        void ManipulateMe_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            e.Handled = true;
        }
        void ManipulateMe_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
        {
            e.Handled = true;
        }
        void ManipulateMe_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }
            _previousTransform.Matrix = _transformGroup.Value;
            Windows.Foundation.Point center = _previousTransform.TransformPoint(new Windows.Foundation.Point(e.Position.X, e.Position.Y));
            _compositeTransform.CenterX = center.X;
            _compositeTransform.CenterY = center.Y;
            _compositeTransform.Rotation = e.Delta.Rotation;
            _compositeTransform.ScaleX = _compositeTransform.ScaleY = e.Delta.Scale;
            _compositeTransform.TranslateX = e.Delta.Translation.X;
            _compositeTransform.TranslateY = e.Delta.Translation.Y;
            e.Handled = false;
        }
        void ManipulateMe_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
         
            e.Handled = true;
        }
    }
}
